    <?php
    /**
    * 奇门接口入口
    * @author fengcong
    * @date 2018.03.26
    * @copyright qianjiangcloud.com
    */
    class ome_qimen 
    {
         
        public function display(){
        //exit(json_encode(array("sub_message"=>"Illegal request","flag"=>"failure","sub_code"=>"sign-check-failure",)));
            try{
                $method_array=array(
                    'oms.orders.add'=>array('ome_ctl_admin_esbapi','api_import'),
                    'oms.orders.add.test'=>array('ome_ctl_admin_esbapi','api_import_test'),
                    'oms.orders.cancel'=>array('ome_ctl_admin_esbapi','order_dead'),
                    'oms.orders.status'=>array('ome_ctl_admin_esbapi','order_status'),
                    'oms.orders.pdatooms'=>array('ome_ctl_admin_esbapi','PDATOOMS'),
                    'oms.orders.pdatoomsship'=>array('ome_ctl_admin_esbapi','PDATOOMSship'),
                    'oms.orders.pdatologin'=>array('ome_ctl_admin_esbapi','PDAToLogin'),
                    'oms.orders.pdatoomsbatch'=>array('ome_ctl_admin_esbapi','PDATOOMSbatchCheckAndDelivery'),
                    'oms.orders.getstocknoticevalue'=>array('ome_ctl_admin_esbapi','GetStocknoticevalue'),
                    'oms.iso.add'=>array('openapi_data_original_transfer','add'),
                    'oms.material.basic'=>array('openapi_data_original_basicmaterial','getList'),
                    'oms.orders.getlist'=>array('openapi_data_original_externalsys','getorderlist'),
                    'oms.delivery.getlist'=>array('openapi_data_original_externalsys','getdeliverylist'),
                );
                $datas=array_merge($_GET,$_POST);
                error_log(date('Y-m-d H:i:s').'接受数据1/'.print_r($datas,1),3,DATA_DIR.'/api_esbapi_orders_'.date('Y-m-d').'.log');
                $_POST['api_data']=$datas['api_data'];
                $_POST['_token']=$datas['oms_token'];
                $method=$datas['oms_method'];
                $dispatch_class_name=$method_array[$method];
                if(class_exists($dispatch_class_name[0])){
                    $dispatchController = kernel::single($dispatch_class_name[0]);
                    if($method=='oms.iso.add')
                    {
                        $params=json_decode($_POST['api_data'],true);
                        $data['name'] = $params['name'];
                        $data['vendor'] = $params['vendor'];
                        $data['type'] = $params['t_type'];
                        $data['branch_bn'] = $params['branch_bn'];
                        $data['delivery_cost'] = $params['delivery_cost'];
                        $data['operator'] = $params['operator'];
                        $data['memo'] = $params['memo'];
                        $data['unique_id'] = $params['unique_id'];
                        $data['confirm'] = $params['confirm'];
                        $data['create_time'] = $params['create_time'];
                        $data['items'] = json_decode($params['items'],true);
                        $res=$dispatchController->$dispatch_class_name[1]($data);
                        exit(json_encode(array('data'=>$res)));
                    }elseif($method=='oms.material.basic')
                    {
                        //add by ldward 添加基础物料获取接口匹配

                        $params=json_decode($_POST['api_data'],true);

                        if ($params['material_bn']) {
                            $filter['material_bn'] = $params['material_bn'];
                        }

                        $page_no = intval($params['page_no']) > 0 ? intval($params['page_no']) : 1;
                        $limit = (intval($params['page_size']) > 100 || intval($params['page_size']) <= 0) ? 40 : intval($params['page_size']);

                        if($page_no == 1){
                            $offset = 0;
                        }else{
                            $offset = ($page_no-1)*$limit;
                        }
                        $res=$dispatchController->$dispatch_class_name[1]($filter,$offset,$limit);
                        exit(json_encode(array('data'=>json_encode($res))));
                    }elseif($method=='oms.orders.getlist')
                    {
                        //add by ldward 查询订单信息

                        $params=json_decode($_POST['api_data'],true);

                        if ($params['order_bn']) {
                            $filter['order_bn'] = $params['order_bn'];
                        }
                        if ($params['relate_order_bn']) {
                            $filter['relate_order_bn'] = $params['relate_order_bn'];
                        } 
                        if ($params['create_starttime']) {
                            $filter['create_starttime'] = $params['create_starttime'];
                        }
                        if ($params['create_endtime']) {
                            $filter['create_endtime'] = $params['create_endtime'];
                        }
                        if ($params['shop_id']) {
                            $filter['shop_id'] = $params['shop_id'];
                        }

                        $page_no = intval($params['page_no']) > 0 ? intval($params['page_no']) : 1;
                        $limit = (intval($params['page_size']) > 100 || intval($params['page_size']) <= 0) ? 40 : intval($params['page_size']);

                        if($page_no == 1){
                            $offset = 0;
                        }else{
                            $offset = ($page_no-1)*$limit;
                        }
                        $res=$dispatchController->$dispatch_class_name[1]($filter,$offset,$limit);
                        exit(json_encode(array('data'=>json_encode($res))));
                    }elseif($method=='oms.delivery.getlist')
                    {
                        //add by ldward 查询发货单清单

                        $params=json_decode($_POST['api_data'],true);

                        if ($params['order_bn']) {
                            $filter['order_bn'] = $params['order_bn'];
                        } 
                        if ($params['relate_order_bn']) {
                            $filter['relate_order_bn'] = $params['relate_order_bn'];
                        } 
                        if ($params['create_starttime']) {
                            $filter['create_starttime'] = $params['create_starttime'];
                        } 
                        if ($params['create_endtime']) {
                            $filter['create_endtime'] = $params['create_endtime'];
                        } 
                        if ($params['shop_id']) {
                            $filter['shop_id'] = $params['shop_id'];
                        } 
                        if ($params['delivery_bn']) {
                            $filter['delivery_bn'] = $params['delivery_bn'];
                        } 
                        if ($params['logi_no']) {
                            $filter['logi_no'] = $params['logi_no'];
                        } 
                        $page_no = intval($params['page_no']) > 0 ? intval($params['page_no']) : 1;
                        $limit = (intval($params['page_size']) > 100 || intval($params['page_size']) <= 0) ? 40 : intval($params['page_size']);

                        if($page_no == 1){
                            $offset = 0;
                        }else{
                            $offset = ($page_no-1)*$limit;
                        }
                        $res=$dispatchController->$dispatch_class_name[1]($filter,$offset,$limit);
                        exit(json_encode(array('data'=>json_encode($res))));
                    }else{
                        $res=$dispatchController->$dispatch_class_name[1]();

                        error_log(date('Y-m-d H:i:s').'返回数据1/'.print_r((json_encode(array('data'=>$res))),1),3,DATA_DIR.'/api_esbapi_orders_'.date('Y-m-d').'.log');
                        exit(json_encode(array('data'=>$res)));
                    }
                    //call_user_func(array($dispatchController, $dispatch_class_name[1]));
                }
            }catch(Exception $e){
                exit(json_encode(array('data'=>'错误')));
            }
        }

        function decodeUnicode($str)
        {
            return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
                create_function(
                    '$matches',
                    'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
                ),
                $str);
        }
    }

